# -*- coding: utf-8 -*-
# @Time        :2025/3/5 上午10:13
# @Author      :文刀水寿
# @File        : 15_爬虫_urllib_handler处理器的基本使用.py
"""
 @Description :动态cookie
"""
# 需求 使用hadnler来访问百度 获取网页源码

import urllib.request

url = 'https://www.baidu.com'

headers = {
    'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36 Edg/133.0.0.0",
}

request = urllib.request.Request(url=url, headers=headers)

# handler build_opener open
# 1.获取handler对象
handler = urllib.request.HTTPHandler()

# 2.获取opener对象
opener = urllib.request.build_opener(handler)

# 3. 调用open方法
response = opener.open(request)

content = response.read().decode('utf8')

print(content)
